//var logger = require('../conf/log4').logger('crawlRank');
var iconv = require('iconv-lite');
var BufferHelper = require('bufferhelper');
//var moment = require('moment');
var cheerio = require('cheerio');
//var client = require("../conf/redis").client;

var url = "http://sclub.jd.com/comment/productPageComments.action?productId=2335348&score=0&sortType=3&pageSize=10&page=";

//替换所有
/*String.prototype.replaceAll  = function(s1,s2){
    return this.replace(new RegExp(s1,"gm"),s2);
};*/

function pcComment() {

   /* /!*if(k>10) return;
    get(url+k,function (data) {
        var obj = JSON.parse(data);
        var comments = obj.comments;
        for(var i in comments){
            var time = moment(comments[i].creationTime);

            console.log("第"+k+"页:"+i+" -->"+comments[i].nickname+":"+comments[i].content.trim().replaceAll("\n","")+",creationTime:");
        }
        pcComment(k+1);
    })*!/
    var s = "2016-09-13 13:12:48";
    var time = moment(s);
    console.log(time.unix())*/


   var url = "https://www.amazon.com/s/?field-keywords=iphone+7+plus+case";
   get(url,function (data) {
       //console.log(data)
       var $ = cheerio.load(data);// 创建DOM操作对象
       $("#atfResults li").each(function () {
           console.log($(this).attr("data-asin"));
       });
       //var html = $("#results-atf-next");
      // console.log(html)
   })
}

pcComment();

function get(url,fn){
    var parse_u=require('url').parse(url,true);
    var isHttp=parse_u.protocol=='http:';
    var options={
        host:parse_u.hostname,
        port:parse_u.port||(isHttp?80:443),
        path:parse_u.path,
        method:'GET',
        headers:{
            'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36'
        }
    };
    var req = require(isHttp?'http':'https').request(options,function(res){
        var charset = "UTF-8";
        var content_type = res.headers['content-type'];
        if(content_type.indexOf("charset")){
            charset = content_type.split("=")[1];
        }
        var bufferHelper = new BufferHelper();
        var _data = "";
        res.on('data', function(chunk){
            bufferHelper.concat(chunk);
            _data += chunk;
        });
        res.on('end', function(){
            //var str = iconv.decode(bufferHelper.toBuffer(),charset);
            //var str = bufferHelper.toBuffer();
            if(fn) fn(_data);
        });
    });
    req.end();
}








function post(url,data,fn){
    data=data||{};
    var content=require('querystring').stringify(data);
    var parse_u=require('url').parse(url,true);
    var isHttp=parse_u.protocol=='http:';
    var options={
        host:parse_u.hostname,
        port:parse_u.port||(isHttp?80:443),
        path:parse_u.path,
        method:'POST',
        headers:{
            'Content-Type':'application/x-www-form-urlencoded',
            'Content-Length':content.length
        }
    };
    var req = require(isHttp?'http':'https').request(options,function(res){
        var _data='';
        res.on('data', function(chunk){
            _data += chunk;
        });
        res.on('end', function(){
            if(fn) fn(_data);
        });
    });
    req.write(content);
    req.end();
}